﻿activityFeed = {};

activityFeed.HttpRequestBuilder = function (url) {
    var self = this;

    var parameters = new Array();

    this.addParameter = function (name, value, defaultValue) {
        parameters.push({ name: name, value: value, defaultValue: defaultValue });
    };

    this.createUrlWithQueryString = function () {
        var qs = "";
        for (var i = 0, length = parameters.length; i < length; i++) {
            var parameter = parameters[i];

            if (!parameter.value) {
                continue;
            }
            if (parameter.defaultValue && parameter.value == parameter.defaultValue) {
                continue;
            }
            qs += encodeURIComponent(parameter.name) + "=" + encodeURIComponent(parameter.value) + "&";
        }

        if (qs.length > 0) {
            qs = qs.substring(0, qs.length - 1); //chop off last "&"
            url = url + "?" + qs;
        }

        return url;
    };
};

activityFeed.api = new function () {
    var self = this;

    // updates
    this.postUpdate = function (statusUpdateText, source, newTopics) {
        return self.callService($.postJSON, '/activityFeed/updates', { text: statusUpdateText, source: source, newTopics: newTopics });
    };

    this.removeUpdate = function (update) {
        return self.callService($.deleteJSON, '/activityFeed/updates/' + encodeURIComponent(update.id()));
    };

    this.getUpdates = function (feedType, customFeedTypeName, searchTerm, dateFrom, pageSize, topicsFilter) {
        /** @const */var DEFAULT_FEED_TYPE = 'personal';
        /** @const */var DEFAULT_PAGE_SIZE = 10;

        if (!topicsFilter) {
            topicsFilter = [];
        }

        var httpRequestBuilder = new activityFeed.HttpRequestBuilder('/activityFeed/updates');
        httpRequestBuilder.addParameter('dateFrom', dateFrom);
        httpRequestBuilder.addParameter('pageSize', pageSize, DEFAULT_PAGE_SIZE);
        httpRequestBuilder.addParameter('searchTerm', searchTerm);
        httpRequestBuilder.addParameter('feedType', feedType, DEFAULT_FEED_TYPE);
        httpRequestBuilder.addParameter('customFeedTypeName', customFeedTypeName, '');
        var topics = topicsFilter.join(';');
        httpRequestBuilder.addParameter('topicsFilter', topics);

        return self.callService($.getJSON, httpRequestBuilder.createUrlWithQueryString());
    };

    // subscribe
    this.subscribe = function (topic) {
        return self.callService($.postJSON, '/activityFeed/subscriptions', { topic: topic });
    };

    this.unsubscribe = function (topic) {
        return self.callService($.deleteJSON, '/activityFeed/subscriptions/' + encodeURIComponent(topic));
    };



    //comments
    this.postComment = function (updateId, commentText) {
        return self.callService($.postJSON, '/activityFeed/updates/' + encodeURIComponent(updateId) + '/comments', { text: commentText });
    };

    this.removeComment = function (updateId, commentId) {
        return self.callService($.deleteJSON, '/activityFeed/updates/' + encodeURIComponent(updateId) + '/comments/' + encodeURIComponent(commentId));
    };


    // likes
    this.likeUpdate = function (updateId) {
        return self.callService($.postJSON, '/activityFeed/updates/' + encodeURIComponent(updateId) + '/likes', {});
    };

    this.removeLikeFromUpdate = function (updateId) {
        return self.callService($.deleteJSON, '/activityFeed/updates/' + encodeURIComponent(updateId) + '/likes');
    };



    // private
    this.callService = function () {
        var functionToCall = arguments[0];
        return functionToCall.apply(this, Array.prototype.slice.call(arguments).skip(1)).fail(self.handleError);
    };

    this.handleError = function (exception) {
        var errorText = "ERROR: status - " + exception.status + ", message: " + exception.statusText;
        alert(errorText);
    };
};